require(cairo)
CairoWin(width = 7, height = 7, pointsize = 12,
	record = getOption("graphics.record"),
	rescale = c("R", "fit", "fixed"), xpinch, ypinch, bg =
	"transparent", canvas = "white", gamma = getOption("gamma"),
	xpos = NA, ypos = NA, buffered = getOption("windowsBuffered"),
	restoreConsole = FALSE)

## definitions
#ts1.name <- "trajectory1_devtime_approx.csv"
#ts2.name <- "trajectory2_devtime_approx.csv"
ts1.name <- "series1.lpt.csv"
ts2.name <- "series2.lpt.csv"

## load the data
ts1.data <- as.vector(read.csv(file=ts1.name, header=FALSE, sep = ",", quote="\"", dec=".", fill = TRUE, comment.char=""))
ts2.data <- as.vector(read.csv(file=ts2.name, header=FALSE, sep = ",", quote="\"", dec=".", fill = TRUE, comment.char=""))

## normalize the data
ts.max <- max(max(ts1.data), max(ts2.data))
dist<-dist(rbind(t(ts1.data/ts.max),t(ts2.data/ts.max)), method= "euclidean")

#ts1.data <- rbind(ts1.data/ts.max, 0)
ts1.data <- ts1.data/ts.max
ts2.data <- ts2.data/ts.max

dist<-dist(rbind(t(ts1.data/ts.max),t(ts2.data/ts.max)), method= "euclidean")

## get the max series length
ts.length <- max(dim(ts2.data)[1], dim(ts1.data)[1])

## plot the original data
#Cairo(600, 600, file="original.png", type="png", bg="white")
plot(NULL, xlim=c(0,ts.length), ylim=c(0,1), main="Development time for trajectory1 and trajectory2", xlab="Time ticks", ylab="Normalized DevTime")
polygon(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), lwd=0.5, col="#00ff0040")
#points(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), pch=3, cex=1, col="mediumspringgreen")
polygon(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=0.5, col="#0000ff40")
#points(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), pch=18, cex=1.5, lwd=2, col="royalblue")

lines(cbind(c(1, 2),c(0.98, 0.98)), lwd=2, col="#00ff0080")
text(2.2, 0.98, labels="trajectory 1 ", cex=0.9, pos=4)
lines(cbind(c(1, 2),c(0.93, 0.93)), lwd=2, col="#0000ff80")
text(2.2, 0.93, labels="trajectory 2 ", cex=0.9, pos=4)
text(0.78, 0.88, labels=paste("Euclidean distance ", sprintf("%2.1f", dist[1])), cex=1.0, pos=4)
#dev.off()


## do DTW
par(mfrow=c(1,1))
library(dtw)
alignment<-dtw(ts1.data,ts2.data, keep=T);
plot(alignment$index1,alignment$index2,main="Warping function");
dtwPlotThreeWay(alignment, main="Both time series and warping curve")
dtwPlotTwoWay(alignment, main="Both time series and warping curve")
dtwPlotDensity(alignment, main="Trajectory1/Trajectory2: symmetric alignment, no constraints")


#par(mfrow=c(2,1));
wq<-warp(alignment,index.template=FALSE);
wt<-warp(alignment,index.template=TRUE);

Cairo(600, 600, file="warp.png", type="png", bg="white")
plot(NULL,xlim=c(0,ts.length), ylim=c(0,1),  xlab="Time ticks", ylab="Normalized DevTime", main="Warping query")
polygon(cbind( c(0,seq(0,dim(ts1.data)[1]-1),dim(ts1.data)[1]-1),
 c(0, as.vector(ts1.data[,1])[wq], 0)), lwd=0.5, col="#00ff0040")
polygon(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=0.5, col="#0000ff40")

dist<-dist(rbind(t(ts2.data/ts.max),t(as.vector(ts1.data[,1])[wq])), method= "euclidean")

lines(cbind(c(9, 10),c(0.98, 0.98)), lwd=2, col="#00ff0080")
text(10.2, 0.98, labels="trajectory 1 ", cex=0.9, pos=4)
lines(cbind(c(9, 10),c(0.93, 0.93)), lwd=2, col="#0000ff80")
text(10.2, 0.93, labels="trajectory 2 ", cex=0.9, pos=4)
text(8.8, 0.88, labels=paste("Euclidean distance ", sprintf("%2.1f", dist[1])), cex=1.0, pos=4)
dev.off()




plot(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data),pch=18, cex=1.5, lwd=2, col="mediumspringgreen", main="Warping template")
lines(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), lwd=2, col="mediumspringgreen");
lines(cbind(seq(0,dim(ts2.data)[1]-1), as.vector(ts2.data[,1])[wt]), lwd=2, col="royalblue");

######################################
######################################
# [2.0] CONTROL SECTION, INPUT DATA
#
#
par(mfrow=c(1,2));

## plot the original data
plot(NULL, xlim=c(0,ts.length), ylim=c(0,1), main="R input", xlab="Time ticks", ylab="Normalized DevTime")
lines(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), lwd=2, col="mediumspringgreen")
points(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), pch=3, cex=1, col="mediumspringgreen")
lines(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=2, col="royalblue")
points(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), pch=18, cex=1.5, lwd=2, col="royalblue")

lines(cbind(c(0, 2),c(0.95, 0.95)), lwd=2, col="mediumspringgreen")
text(2.2, 0.95, labels="trajectory1", cex=0.9, pos=4)
lines(cbind(c(0, 2),c(0.9, 0.9)), lwd=2, col="royalblue")
text(2.2, 0.9, labels="trajectory2", cex=0.9, pos=4)

## plot the Java extracted data
b_1<-c(0.00,0.00,0.50,0.26,0.31,0.24,0.30,0.00,0.00,0.30,0.26,0.24,0.33,0.28,0.00)
b_2<-c(0.00,0.22,0.22,0.28,0.00,0.00,0.83,0.93,1.00,0.81,0.81,0.00,0.00,0.26,0.22)
plot(NULL, xlim=c(0,ts.length), ylim=c(0,1), main="Java input", xlab="Time ticks", ylab="Normalized DevTime")
lines(cbind(seq(0,dim(ts2.data)[1]-1), b_2), lwd=2, col="mediumspringgreen")
points(cbind(seq(0,dim(ts2.data)[1]-1), b_2), pch=3, cex=1, col="mediumspringgreen")
points(cbind(seq(0,dim(ts1.data)[1]-1), b_1),pch=18, cex=1.5, lwd=2, col="royalblue")
lines(cbind(seq(0,dim(ts1.data)[1]-1), b_1), lwd=2, col="royalblue");
lines(cbind(c(0, 2),c(0.95, 0.95)), lwd=2, col="mediumspringgreen")
text(2.2, 0.95, labels="trajectory1", cex=0.9, pos=4)
lines(cbind(c(0, 2),c(0.9, 0.9)), lwd=2, col="royalblue")
text(2.2, 0.9, labels="trajectory2", cex=0.9, pos=4)

######################################
######################################
# [3.0] CONTROL SECTION, OUTPUT DATA
#
#
par(mfrow=c(1,2));
wq<-warp(alignment,index.template=FALSE);

## plot the original data
plot(NULL, xlim=c(0, ts.length), ylim=c(0,1), main="Warping query, R", xlab="Time ticks", ylab="Normalized DevTime")
lines(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=2, col="royalblue")
points(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), pch=18, cex=1.5, lwd=2, col="royalblue")
points(cbind(seq(0,dim(ts1.data)[1]-1), as.vector(ts1.data[,1])[wq]), pch=3, cex=1, col="mediumspringgreen")
lines(cbind(seq(0,dim(ts1.data)[1]-1), as.vector(ts1.data[,1])[wq]), lwd=2, col="mediumspringgreen");

lines(cbind(c(5, 7),c(0.98, 0.98)), lwd=2, col="mediumspringgreen")
text(7.2, 0.98, labels="trajectory1, QUERY", cex=0.9, pos=4)
lines(cbind(c(5, 7),c(0.93, 0.93)), lwd=2, col="royalblue")
text(7.2, 0.93, labels="trajectory2, TEMPLATE", cex=0.9, pos=4)



## plot the Java extracted data
a_1<-c(0.00,0.00,0.00,0.00,0.00,0.00,0.50,0.26,0.31,0.24,0.30,0.00,0.00,0.24,0.0)
a_2<-c(0.22,0.00,0.83,0.93,1.00,0.81,0.81,0.00,0.00,0.26,0.26,0.26,0.26,0.26,0.22)
plot(NULL, xlim=c(0, ts.length), ylim=c(0,1), main="Warping query, R", xlab="Time ticks", ylab="Normalized DevTime")
points(cbind(seq(0,dim(ts1.data)[1]-1), b_1),pch=18, cex=1.5, lwd=2, col="royalblue")
lines(cbind(seq(0,dim(ts1.data)[1]-1), b_1), lwd=2, col="royalblue");
